from typing import Dict, List

from django import views


def format_category_view(views: int):
    if views > 10000_0000:
        return str(views // 1000_0000/10)+"亿"
    if views > 1_0000:
        return str(views // 1000/10)+"万"
    return views


def group_course_by_category(courses: List):
    course_dict = {}
    views_dict = {}
    for course in courses:
        if course.category not in course_dict:
            course_dict[course.category] = []
            views_dict[course.category] = 0

        course_dict[course.category].append(course)
        views_dict[course.category] += course.views

    for category in views_dict:
        views_dict[category] = format_category_view(views_dict[category])
    return course_dict, views_dict
